// singer.entity.ts
import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  OneToMany,
  PrimaryColumn,
  ManyToMany,
  ManyToOne,
} from 'typeorm';
import { Poll } from './poll.entity';
import * as crypto from 'crypto';
import { ApiProperty } from '@nestjs/swagger';
import { VoteRecord } from './vote-record.entity';
import { BaseEntity } from './base.entity';
import { CommentSinger } from './comment.entity';

@Entity()
export class Singer extends BaseEntity {
  constructor() {
    super();
    this.id = crypto.randomBytes(8).toString('hex'); // 生成一个8字节（16个十六进制字符）的随机字符串作为id
  }
  @PrimaryColumn({ length: 225 })
  @ApiProperty({
    required: false,
    description: '歌手id',
  })
  id: string;

  @ApiProperty({
    required: false,
    description: '歌手名称',
  })
  @Column({ type: 'varchar', length: 255, comment: '歌手名称' })
  name: string;

  @ApiProperty({
    required: false,
    description: '歌曲名',
  })
  @Column({ type: 'varchar', length: 255, comment: '歌曲名', nullable: true })
  musicName: string;

  @ApiProperty({
    required: false,
    description: '歌曲URL',
  })
  @Column({ type: 'varchar', length: 255, comment: '歌曲URL', nullable: true })
  musicUrl: string;

  @ApiProperty({
    required: false,
    description: '简介',
  })
  @Column({ type: 'varchar', length: 255, comment: '简介' , nullable: true})
  introduction: string;

  @Column({ type: 'int', default: 0, comment: '投票数量', nullable: true })
  votedCount: number; // 投票数量字段

  @ManyToOne(() => Poll, (poll) => poll.singers, { onDelete: 'SET NULL' })
  poll: Poll;

  @OneToMany(() => VoteRecord, (voteRecord) => voteRecord.singer, {
    cascade: true, // 自动保存相关对象
    onDelete: 'CASCADE',
  })
  voteRecords: VoteRecord[];

  @OneToMany(() => CommentSinger, (CommentSinger) => CommentSinger.singer, {
    cascade: true, // 自动保存相关对象
    onDelete: 'CASCADE',
  })
  comments: CommentSinger[];
}
